<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.prime.com.tr/ui">

    <body>

        <ui:composition template="./../../Template/generalTemplate.xhtml">


            <ui:define name="content">
                <h:form id="main">
                    <f:view beforePhase="#{createRawMaterialPurchasingManagedBean.initView}">
                        <p:dialog widgetVar="dlg" header="Raw Material Requirement" width="1000" showEffect="fade" modal="true" >
                            <p:outputPanel style="margin-top:10px; margin-left:5px; width:800px;">   
                                <h:panelGrid columns="2" cellpadding="3">
                                    <h:outputText value="Requirement ID" />  
                                    <h:panelGroup>  
                                        <h:outputText value="#{createRawMaterialPurchasingManagedBean.rawMaterialRequirement.id}" style="font-weight:bold"/>  
                                    </h:panelGroup>  

                                    <h:outputText value="Date Generated" />  
                                    <h:panelGroup>  
                                        <h:outputText value="#{createRawMaterialPurchasingManagedBean.rawMaterialRequirement.dateGenerated}" style="font-weight:bold">
                                            <f:convertDateTime pattern="dd-MM-yyyy" />
                                        </h:outputText>  
                                    </h:panelGroup> 

                                </h:panelGrid>
                                <p:dataTable var="lineItem" value="#{createRawMaterialPurchasingManagedBean.rawMaterialRequirement.rawMaterialRequirementLineItems}" dynamic="true" emptyMessage="The list is empty">
                                    <p:column>
                                        <f:facet name="header">
                                            <h:outputText value="Material Id" />
                                        </f:facet>
                                        <h:outputText value="#{lineItem.rawMaterial.id}" />
                                    </p:column>

                                    <p:column>
                                        <f:facet name="header">
                                            <h:outputText value="Material Name" />
                                        </f:facet>
                                        <h:outputText value="#{lineItem.rawMaterial.rawMaterialName}" />
                                    </p:column>

                                    <p:column>
                                        <f:facet name="header">
                                            <h:outputText value="Quantity" />
                                        </f:facet>
                                        <h:outputText value="#{lineItem.quantity}" />
                                    </p:column>
                                </p:dataTable>
                            </p:outputPanel>
                        </p:dialog>

                        <p:dialog id="dlg2" widgetVar="dlg2" header="Raw Material Planning" width="800" showEffect="fade" modal="true" >
                            <p:outputPanel id="outputPanelDlg2">
                                <table style="border:black solid 1px;">
                                    <tr>
                                        <td width="200" style="font-weight:bold;">
                                            <h:outputText value="Material Name: #{createRawMaterialPurchasingManagedBean.rawMaterial.rawMaterialName}" /> 
                                        </td>
                                        <td width="200" style="font-weight:bold;">
                                            <h:outputText value="Lead Time: #{createRawMaterialPurchasingManagedBean.rawMaterial.leadTime}" />
                                        </td>
                                        <td width="200" style="font-weight:bold;">
                                            <h:outputText value="Est. Quantity Left: #{createRawMaterialPurchasingManagedBean.estQuantityLeft}" />
                                        </td>
                                    </tr>
                                    <hr/>
                                    <tr>
                                        <td width="150" style="font-weight:bold;">
                                            <h:outputText value="Lot Size: #{createRawMaterialPurchasingManagedBean.rawMaterial.lotSize} "/>
                                        </td>
                                        <td width="150" style="font-weight:bold;" >
                                            <h:outputText value="Week 1"/>
                                        </td>
                                        <td width="150" style="font-weight:bold;">
                                            <h:outputText value="Week 2"/>
                                        </td>
                                        <td width="150" style="font-weight:bold;">
                                            <h:outputText value="Week 3"/>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td style="font-weight:bold;">
                                            <h:outputText value="Gross Requirements:"/>
                                        </td>
                                        <td>
                                            <h:outputText value="#{createRawMaterialPurchasingManagedBean.weekOneRequired}"/>
                                        </td>
                                        <td >
                                            <h:outputText value="#{createRawMaterialPurchasingManagedBean.weekTwoRequired}"/>
                                        </td>
                                        <td>
                                            <h:outputText value ="#{createRawMaterialPurchasingManagedBean.weekThreeRequired}"/>   
                                        </td>
                                    </tr>
                                    <tr>
                                        <td style="font-weight:bold;">
                                            <h:outputText value="Safety Stock:"/>
                                        </td>
                                        <td>
                                            <h:outputText value="#{createRawMaterialPurchasingManagedBean.getSafetyStock()}"/> 
                                        </td>
                                        <td >
                                            <h:outputText value="#{createRawMaterialPurchasingManagedBean.getSafetyStock()}"/> 
                                        </td>
                                        <td>
                                            <h:outputText value="#{createRawMaterialPurchasingManagedBean.getSafetyStock()}"/> 
                                        </td>
                                    </tr>
                                    <tr>
                                        <td style="font-weight:bold;">
                                            <h:outputText value="Scheduled Receipts:"/>
                                        </td>
                                        <td>
                                            <h:outputText value="#{createRawMaterialPurchasingManagedBean.weekOneIncoming}"/>
                                        </td>
                                        <td >
                                            <h:outputText value="#{createRawMaterialPurchasingManagedBean.weekTwoIncoming}"/>
                                        </td>
                                        <td>
                                            <h:outputText value ="#{createRawMaterialPurchasingManagedBean.weekThreeIncoming}"/>   
                                        </td>
                                    </tr>

                                    <tr>
                                        <td style="font-weight:bold;">
                                            <h:outputText value="On Hand: #{createRawMaterialPurchasingManagedBean.currentRawMaterialInventoryLevel}"/>
                                        </td>
                                        <td>
                                            <h:outputText value="#{createRawMaterialPurchasingManagedBean.weekOneEstQuantityLeft}" />
                                        </td>
                                        <td>
                                            <h:outputText value="#{createRawMaterialPurchasingManagedBean.weekTwoEstQuantityLeft}" />

                                        </td>
                                        <td>
                                            <h:outputText value="#{createRawMaterialPurchasingManagedBean.weekThreeEstQuantityLeft}"/>

                                        </td>
                                    </tr>

                                    <tr>
                                        <td style="font-weight:bold;">
                                            <h:outputText value="Planned Orders:"/>
                                        </td>

                                        <td>
                                            <h:outputText value="#{createRawMaterialPurchasingManagedBean.weekonePlan}"/>  
                                        </td>

                                        <td >
                                            <h:outputText value="#{createRawMaterialPurchasingManagedBean.weektwoPlan}"/>
                                        </td>

                                        <td>
                                            <h:outputText value="#{createRawMaterialPurchasingManagedBean.weekthreePlan}"/>
                                        </td>
                                    </tr>

                                </table>
                            </p:outputPanel>
                        </p:dialog>

                        <p style="color:slategray; font:13px; font-weight:bold">Purchase Order Line Item List</p>
                        <p:dataTable  id ="lineItemList" paginator="true" rows="6" dynamic="true" emptyMessage="List is currently empty" value="#{createRawMaterialPurchasingManagedBean.purchasingList}" var="o" >


                            <p:column filterBy="#{o.rawMaterial.id}" filterMatchMode="contains">
                                <f:facet name="header">Material Id</f:facet>
                                #{o.rawMaterial.id}
                            </p:column> 

                            <p:column filterBy="#{o.rawMaterial.rawMaterialName}" filterMatchMode="contains" >
                                <f:facet name="header">Material Name</f:facet>
                                #{o.rawMaterial.rawMaterialName}
                            </p:column> 

                            <p:column filterBy="#{o.supplierLineItemEntity.supplier.supplierName}" filterMatchMode="contains">
                                <f:facet name="header">Supplier</f:facet>
                                #{o.supplierLineItemEntity.supplier.supplierName}
                            </p:column> 

                            <p:column>
                                <f:facet name="header">Lot Size</f:facet>
                                #{o.rawMaterial.lotSize}
                            </p:column>

                            <p:column>
                                <f:facet name="header">Lot Purchased</f:facet>
                                #{o.lotQuantity}
                            </p:column>

                            <p:column>
                                <f:facet name="header">Quantity</f:facet>
                                #{o.totalQuantity}
                            </p:column>

                            <p:column>
                                <f:facet name="header">Price</f:facet>
                                #{o.supplierLineItemEntity.price}
                            </p:column>

                            <p:column>
                                <f:facet name="header">Cum Amount</f:facet>
                                #{o.cumAmount}
                            </p:column>

                            <p:column>
                                <f:facet name="header">Edit</f:facet>
                                <p:commandLink value ="Delete" update ="lineItemList" ajax ="false" action="#{createRawMaterialPurchasingManagedBean.deleteLineItem(o)}"/>
                            </p:column>        
                        </p:dataTable>

                        <p:panel id="lineItemAddition" style="margin-top:25px; " header="Add The LineItem" toggleable="true">
                            <table>
                                <tr>
                                <td width="150"><h:selectOneMenu id="productType" onchange="#{createRawMaterialPurchasingManagedBean.updateRawMaterialFields()}" immediate="true" value="#{createRawMaterialPurchasingManagedBean.rawMaterialId}">  
                                        <f:selectItems value="#{createRawMaterialPurchasingManagedBean.getRequiredRawMaterial()}" />
                                        <p:ajax update="main" />
                                    </h:selectOneMenu>
                                </td>

                                <td  width="200"><h:selectOneMenu  value="#{createRawMaterialPurchasingManagedBean.supplierLineItemId}" rendered="#{createRawMaterialPurchasingManagedBean.render}">  
                                        <f:selectItems value="#{createRawMaterialPurchasingManagedBean.listOfSuppliers}" />
                                    </h:selectOneMenu>
                                </td>

                                <td  width="200"><h:outputText value = "Required Quantity"/>
                                    <font style="color:blue;"><h:outputText value="#{createRawMaterialPurchasingManagedBean.requiredQuantity}"/></font>
                                </td>

                                <td  width="200"><h:outputText value = "Est Quantity Left"/>
                                    <font style="color:blue;"><h:outputText value="#{createRawMaterialPurchasingManagedBean.estQuantityLeft}"/></font>
                                </td>

                                <td  width="300"><h:outputText value = "Planned Ordering Quantity"/>
                                    <font style="color:blue;"><h:outputText value="#{createRawMaterialPurchasingManagedBean.plannedOrderingQuantity}"/></font>
                                </td>
                                 </tr>
                                <tr>
                                <td  width="200"><h:outputText value = "Lot Size"/>
                                    <font style="color:blue;"><h:outputText value="#{createRawMaterialPurchasingManagedBean.lotSize}"/></font>
                                </td>

                                <td  width="200"><h:outputText value = "Price"/>
                                    <font style="color:blue;"><h:outputText value="#{createRawMaterialPurchasingManagedBean.unitPrice}"/></font>
                                </td>

                                <td  width="200"><h:outputText value = "No. of lots"/>
                                    <h:inputText id="a" value="#{createRawMaterialPurchasingManagedBean.lotQuantity}" />
                                </td>

                                <td  width="200"><h:outputText value = "Total Quantity"/>
                                    <font style="color:blue;"><h:outputText value="#{createRawMaterialPurchasingManagedBean.totalQuantity}"/></font>
                                </td>

                                <td  width="200"><h:outputText value = "Cum Amount"/>
                                    <font style="color:blue;"><h:outputText  id="cumAmount" value="#{createRawMaterialPurchasingManagedBean.cumAmount}" /></font>
                                </td>

                                <p:poll interval="2" actionListener="#{createRawMaterialPurchasingManagedBean.updateFields}" update="lotSize unitPrice cumAmount" />
                                <td  width="150"><p:commandButton value="Add" update="lineItemList growl1 lineItemAddition" ajax ="true" actionListener ="#{createRawMaterialPurchasingManagedBean.addLineItem}"/></td>
                           </tr>
                            </table>

                        </p:panel>

                        <p:commandButton value="Generate Purchase Orders" ajax ="true" actionListener ="#{createRawMaterialPurchasingManagedBean.generatePurchaseOrders}"/>
                        <p:commandButton value="View Requirement Detail" ajax ="true" onclick="dlg.show()"/>
                        <p:commandButton value="Generate Line Items" update="lineItemList growl1 lineItemAddition" ajax ="true" actionListener ="#{createRawMaterialPurchasingManagedBean.autoGeneratePurchasingOrders}"/>
                        <p:commandButton value="Planning Details" immediate="true" oncomplete="main:dlg2.show()" update="main:outputPanelDlg2" ajax ="true" actionListener ="#{createRawMaterialPurchasingManagedBean.displayRawMaterialPlanning}"/>

                    </f:view>
                </h:form>

            </ui:define>

        </ui:composition>

    </body>
</html>
